package com.dlust.myplayer

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d("MainActivity", "创建MainActivity")
    
    setContent {
      MaterialTheme {
        // 记录URL切换，用于提示用户
        var lastSwitchTime by remember { mutableStateOf(0L) }
        var showSwitchInfo by remember { mutableStateOf(false) }
        
        // 监听URL变化
        LaunchedEffect(MediaConfig.currentVideoUrl.value) {
          lastSwitchTime = System.currentTimeMillis()
          showSwitchInfo = true
        }
        
        // 3秒后隐藏切换提示
        LaunchedEffect(lastSwitchTime) {
          if (lastSwitchTime > 0) {
            kotlinx.coroutines.delay(3000)
            showSwitchInfo = false
          }
        }
        
        Column(
          modifier = Modifier
            .verticalScroll(rememberScrollState())
            .padding(bottom = 16.dp)
        ) {
          // 视频类型选择器
          VideoTypeSelector()
          
          // 切换提示
          if (showSwitchInfo) {
            Card(
              modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 8.dp, vertical = 4.dp),
              colors = CardDefaults.cardColors(
                containerColor = Color(0xFFE1F5FE)
              )
            ) {
              Text(
                text = "正在切换到: ${extractFileName(MediaConfig.currentVideoUrl.value)}",
                modifier = Modifier
                  .fillMaxWidth()
                  .padding(8.dp),
                fontSize = 14.sp,
                textAlign = TextAlign.Center,
                color = Color(0xFF0277BD)
              )
            }
          }
          
          Spacer(modifier = Modifier.height(8.dp))

          Text("MediaPlayer实现", modifier = Modifier.padding(8.dp))
          MediaPlayerScreen(
            modifier = Modifier
              .fillMaxWidth()
              .height(240.dp)
          )
          
          // 显示MediaPlayer播放的URL文件名
          Text(
            text = "当前播放: ${extractFileName(MediaConfig.currentVideoUrl.value)}",
            modifier = Modifier
              .fillMaxWidth()
              .padding(horizontal = 8.dp, vertical = 4.dp),
            fontSize = 14.sp,
            textAlign = TextAlign.Center
          )

          Spacer(modifier = Modifier.height(16.dp))

          Text("ExoPlayer3实现", modifier = Modifier.padding(8.dp))
          ExoPlayerScreen(
            modifier = Modifier
              .fillMaxWidth()
              .height(240.dp)
          )
          
          // 显示ExoPlayer播放的URL文件名
          Text(
            text = "当前播放: ${extractFileName(MediaConfig.currentVideoUrl.value)}",
            modifier = Modifier
              .fillMaxWidth()
              .padding(horizontal = 8.dp, vertical = 4.dp),
            fontSize = 14.sp,
            textAlign = TextAlign.Center
          )
        }
      }
    }
  }

  override fun onDestroy() {
    super.onDestroy()
    Log.d("MainActivity", "销毁MainActivity")
  }
  
  // 从URL中提取文件名
  private fun extractFileName(url: String): String {
    // 将URL按"/"分割，取最后一部分作为文件名
    val parts = url.split("/")
    return if (parts.isNotEmpty()) parts.last() else url
  }
}

@Composable
fun VideoTypeSelector() {
  var expanded by remember { mutableStateOf(false) }
  val currentType by MediaConfig.currentVideoType
  
  Card(
    modifier = Modifier
      .fillMaxWidth()
      .padding(8.dp),
    elevation = CardDefaults.cardElevation(
      defaultElevation = 2.dp
    )
  ) {
    Row(
      modifier = Modifier
        .fillMaxWidth()
        .clickable { expanded = true }
        .padding(16.dp),
      verticalAlignment = Alignment.CenterVertically
    ) {
      Text(
        text = "选择视频类型: ",
        modifier = Modifier.padding(end = 8.dp)
      )
      
      Box {
        Row(
          verticalAlignment = Alignment.CenterVertically
        ) {
          Text(
            text = currentType.displayName,
            color = MaterialTheme.colorScheme.primary
          )
          Spacer(modifier = Modifier.width(4.dp))
          Icon(
            imageVector = Icons.Default.ArrowDropDown,
            contentDescription = "选择",
            tint = MaterialTheme.colorScheme.primary
          )
        }
        
        DropdownMenu(
          expanded = expanded,
          onDismissRequest = { expanded = false }
        ) {
          MediaConfig.VideoType.values().forEach { type ->
            DropdownMenuItem(
              text = { Text(type.displayName) },
              onClick = {
                MediaConfig.switchVideoType(type)
                expanded = false
              }
            )
          }
        }
      }
    }
  }
}

